package com.ebeitech.auction.push.xiaomi;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;

import com.ebeitech.auction.constants.CommonConstants;
import com.ebeitech.auction.view.homepage.HomepageActivity;
import com.ebeitech.library.constant.SPConstants;
import com.ebeitech.library.util.Log;
import com.ebeitech.library.util.SPManager;
import com.xiaomi.mipush.sdk.ErrorCode;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.xiaomi.mipush.sdk.MiPushCommandMessage;
import com.xiaomi.mipush.sdk.MiPushMessage;
import com.xiaomi.mipush.sdk.PushMessageReceiver;

import java.util.List;

/**
 * 1、PushMessageReceiver是个抽象类，该类继承了BroadcastReceiver。
 * 2、需要将自定义的DemoMessageReceiver注册在AndroidManifest.xml文件中 <receiver
 * android:exported="true"
 * android:name="com.xiaomi.mipushdemo.DemoMessageReceiver"> <intent-filter>
 * <action android:name="com.xiaomi.mipush.RECEIVE_MESSAGE" /> </intent-filter>
 * <intent-filter> <action android:name="com.xiaomi.mipush.ERROR" />
 * </intent-filter> <intent-filter>
 *  <action android:name="com.xiaomi.mipush.MESSAGE_ARRIVED" /></intent-filter>
 *  </receiver>
 * 3、DemoMessageReceiver的onReceivePassThroughMessage方法用来接收服务器向客户端发送的透传消息
 * 4、DemoMessageReceiver的onNotificationMessageClicked方法用来接收服务器向客户端发送的通知消息，
 * 这个回调方法会在用户手动点击通知后触发
 * 5、DemoMessageReceiver的onNotificationMessageArrived方法用来接收服务器向客户端发送的通知消息，
 * 这个回调方法是在通知消息到达客户端时触发。另外应用在前台时不弹出通知的通知消息到达客户端也会触发这个回调函数
 * 6、DemoMessageReceiver的onCommandResult方法用来接收客户端向服务器发送命令后的响应结果
 * 7、DemoMessageReceiver的onReceiveRegisterResult方法用来接收客户端向服务器发送注册命令后的响应结果
 * 8、以上这些方法运行在非UI线程中
 * 
 * @author mayixiang
 */
public class PushReceiverXiaomi extends PushMessageReceiver {
    
    private String mRegId;
//    private long mResultCode = -1;
//    private String mReason;
//    private String mCommand;
//    private String mMessage;
//    private String mTopic;
//    private String mAlias;
//    private String mAccount;
//    private String mStartTime;
//    private String mEndTime;
	private final static int SEND_MY_STATE = -1;
    
    @Override
    public void onReceivePassThroughMessage(Context context, MiPushMessage message){
    	//小米透传消息 未使用
        
    }
    
    @Override
    public void onNotificationMessageClicked(Context context, MiPushMessage message){
        Log.v("onNotificationMessageClicked is called. " + message.toString());
        
        Intent notificationIntent = new Intent(context, HomepageActivity.class);
        context.startActivity(notificationIntent);
    }
    
    @Override
    public void onNotificationMessageArrived(Context context, MiPushMessage message){
    	String content = message.toString();
    	Log.i(getClass(), "小米推送:收到一条推送 " + message.toString());
    	
    }

    @SuppressLint("NewApi")
	@Override
    public void onCommandResult(Context context, MiPushCommandMessage message) {
    	Log.i(getClass(), "小米推送：获取给服务器发送命令的结果");
        String command = message.getCommand();
        List<String> arguments = message.getCommandArguments();
        String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
        String cmdArg2 = ((arguments != null && arguments.size() > 1) ? arguments.get(1) : null);
        if (MiPushClient.COMMAND_REGISTER.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mRegId = cmdArg1;

				SPManager.getInstance().put(SPConstants.SHARED_PRE_CHANNEL_ID_KEY, mRegId);
				SPManager.getInstance().put(SPConstants.SHARED_PRE_CHANNEL_STYLE, "xiaomi");
                
                Log.i("注册成功mRegId:"+mRegId);
                boolean isLoginOffline = SPManager.getInstance().get(CommonConstants.IS_LOGIN_OFFLINE, true);
                if (!isLoginOffline) {
                    PushUtils.sendMyStateUrl(context, SPConstants.LOGIN_STATE);
                }
            } else {
            	 Log.i("注册失败----"+mRegId);
            }
        }
    }
    
    @Override
    public void onReceiveRegisterResult(Context context, MiPushCommandMessage message){
        Log.v("onReceiveRegisterResult is called. " + message.toString());
        String command = message.getCommand();
        List<String> arguments = message.getCommandArguments();
        String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
        if (MiPushClient.COMMAND_REGISTER.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mRegId = cmdArg1;
                Log.i("注册成功mRegId:"+mRegId);
            } else {
            	 Log.i("注册失败----"+mRegId);
            }
        } else {
        }
    }

}
